package service

import (
	"net"
	"pkg/libnet"
)

type ServiceConn struct {
	ID   uint32
	Ch   chan Msg
	IP   []byte
	conn *libnet.TCPConn
	Key  uint32
}

func NewServiceConn(id uint32, conn *libnet.TCPConn, bufSize uint, key uint32) (ac *ServiceConn) {
	ac = new(ServiceConn)
	ac.Ch = make(chan Msg, bufSize)
	ac.IP = make([]byte, 4)
	ac.ID = id
	ac.conn = conn
	ac.Key = key
	if conn != nil {
		copy(ac.IP, []byte(net.ParseIP(conn.ClientIP()))[12:])
	}
	return
}

func (c *ServiceConn) Conn() *libnet.TCPConn {
	return c.conn
}

func (c *ServiceConn) SetConn(conn *libnet.TCPConn) {
	c.conn = conn
}

func (c *ServiceConn) Close() {
	close(c.Ch)
	c.conn.Close()
}
